// MessengerApp.js
//


Type.createNamespace('MessengerApp');

////////////////////////////////////////////////////////////////////////////////
// MessengerApp._contactItem

MessengerApp._contactItem = function MessengerApp__contactItem(contact, contactElement) {
    this._contact = contact;
    this._contactElement = contactElement;
    this._clickHandler = Delegate.create(this, this._onDoubleClick);
    this._contactElement.attachEvent('ondblclick', this._clickHandler);
    this._contact.get_currentAddress().get_presence().add_propertyChanged(Delegate.create(this, this._onPresencePropertyChanged));
    this._updateDisplay();
}
MessengerApp._contactItem.prototype = {
    _contact: null,
    _contactElement: null,
    _clickHandler: null,
    
    _onDoubleClick: function MessengerApp__contactItem$_onDoubleClick() {
        MessengerApp._messengerApplication.current.startConversation(this._contact);
    },
    
    _onPresencePropertyChanged: function MessengerApp__contactItem$_onPresencePropertyChanged(sender, e) {
        this._updateDisplay();
    },
    
    _updateDisplay: function MessengerApp__contactItem$_updateDisplay() {
        var address = this._contact.get_currentAddress();
        var presence = address.get_presence();
        var displayName = presence.get_displayName();
        var addressText = address.get_address();
        var message = presence.get_personalMessage();
        var status = '(Online)';
        switch (presence.get_status()) {
            case Microsoft.Live.Messenger.PresenceStatus.offline:
                status = '(Offline)';
                break;
            case Microsoft.Live.Messenger.PresenceStatus.away:
                status = '(Away)';
                break;
        }
        var sb = new StringBuilder();
        sb.append(displayName || addressText);
        sb.append(' ');
        sb.append(status);
        if (message.length) {
            sb.append(' - ');
            sb.append(message.htmlEncode());
        }
        this._contactElement.innerHTML = sb.toString();
    },
    
    dispose: function MessengerApp__contactItem$dispose() {
        if (this._contactElement) {
            this._contactElement.detachEvent('ondblclick', this._clickHandler);
            this._clickHandler = null;
            this._contactElement = null;
        }
        this._contact = null;
    }
}


////////////////////////////////////////////////////////////////////////////////
// MessengerApp._conversationItem

MessengerApp._conversationItem = function MessengerApp__conversationItem(conversation, conversationElement) {
    this._conversation = conversation;
    this._conversationElement = conversationElement;
    this._conversation.add_messageReceived(Delegate.create(this, this._onMessageReceived));
    this._createUserInterface();
}
MessengerApp._conversationItem.prototype = {
    _conversation: null,
    _conversationElement: null,
    _transcriptElement: null,
    _messageElement: null,
    _sendButtonElement: null,
    _clickHandler: null,
    
    _createUserInterface: function MessengerApp__conversationItem$_createUserInterface() {
        this._clickHandler = Delegate.create(this, this._onSendButtonClick);
        this._transcriptElement = document.createElement('div');
        this._transcriptElement.className = 'conversationTranscript';
        this._messageElement = document.createElement('textarea');
        this._messageElement.className += 'conversationMessage';
        this._sendButtonElement = document.createElement('input');
        this._sendButtonElement.className = 'conversationSendButton';
        this._sendButtonElement.type = 'button';
        this._sendButtonElement.value = 'Send';
        this._sendButtonElement.attachEvent('onclick', this._clickHandler);
        this._conversationElement.appendChild(this._transcriptElement);
        this._conversationElement.appendChild(this._messageElement);
        this._conversationElement.appendChild(this._sendButtonElement);
    },
    
    _addMessage: function MessengerApp__conversationItem$_addMessage(m) {
        var messageElement = document.createElement('div');
        messageElement.className = 'conversationMessageItem';
        var sb = new StringBuilder();
        sb.append(m.get_sender().get_presence().get_displayName());
        sb.append(' says:<br />');
        sb.append('<span class=\"conversationMessageText\">');
        sb.append(m.get_text());
        sb.append('</span>');
        messageElement.innerHTML = sb.toString();
        this._transcriptElement.appendChild(messageElement);
    },
    
    _onMessageReceived: function MessengerApp__conversationItem$_onMessageReceived(sender, e) {
        var m = e.get_message();
        if (m.get_type() === Microsoft.Live.Messenger.MessageType.textMessage) {
            this._addMessage(m);
        }
    },
    
    _onSendButtonClick: function MessengerApp__conversationItem$_onSendButtonClick() {
        var message = new Microsoft.Live.Messenger.TextMessage(this._messageElement.value, null);
        this._messageElement.value = '';
        this._conversation.sendMessage(message, null);
        this._addMessage(message);
    },
    
    dispose: function MessengerApp__conversationItem$dispose() {
        if (this._sendButtonElement) {
            this._sendButtonElement.detachEvent('onclick', this._clickHandler);
            this._sendButtonElement = null;
            this._clickHandler = null;
        }
        this._messageElement = null;
        this._transcriptElement = null;
        this._conversationElement = null;
        this._conversation = null;
    }
}


////////////////////////////////////////////////////////////////////////////////
// MessengerApp._messengerApplication

MessengerApp._messengerApplication = function MessengerApp__messengerApplication() {
    ScriptFX.Application.current.registerDisposableObject(this);
    var signInElement = $('signInPanel');
    var signInControl = new Microsoft.Live.Messenger.UI.SignInControl(signInElement.id, signInElement.getAttribute('privacySource'), signInElement.getAttribute('channelSource'), 'en-US');
    signInControl.add_authenticationCompleted(Delegate.create(this, this._onAuthenticationCompleted));
}
MessengerApp._messengerApplication.prototype = {
    _user: null,
    _contacts: null,
    _conversations: null,
    
    _onAuthenticationCompleted: function MessengerApp__messengerApplication$_onAuthenticationCompleted(sender, e) {
        this._user = new Microsoft.Live.Messenger.User(e.get_identity());
        this._user.add_signInCompleted(Delegate.create(this, this._onSignInCompleted));
        this._user.get_conversations().add_collectionChanged(Delegate.create(this, this._onConversationCollectionChanged));
        this._user.signIn(null);
    },
    
    _onSignInCompleted: function MessengerApp__messengerApplication$_onSignInCompleted(sender, e) {
        if (e.get_resultCode() === Microsoft.Live.Messenger.SignInResultCode.success) {
            var contactListElement = $('contactListPanel');
            this._showContacts(contactListElement);
            $('messengerPanel').style.display = 'block';
        }
    },
    
    _showContacts: function MessengerApp__messengerApplication$_showContacts(contactListElement) {
        this._contacts = [];
        var $enum1 = this._user.get_contacts().getEnumerator();
        while ($enum1.moveNext()) {
            var contact = $enum1.get_current();
            var contactElement = document.createElement('div');
            contactElement.className = 'contactItem';
            contactListElement.appendChild(contactElement);
            var contactItem = new MessengerApp._contactItem(contact, contactElement);
            this._contacts.add(contactItem);
        }
    },
    
    _onConversationCollectionChanged: function MessengerApp__messengerApplication$_onConversationCollectionChanged(sender, e) {
        if (e.get_action() === Microsoft.Live.Core.NotifyCollectionChangedAction.add) {
            var conversationListElement = $('conversationListPanel');
            var conversation = e.get_newItems()[0];
            var conversationElement = document.createElement('div');
            conversationElement.className = 'conversationItem';
            conversationListElement.appendChild(conversationElement);
            if (!this._conversations) {
                this._conversations = [];
            }
            var conversationItem = new MessengerApp._conversationItem(conversation, conversationElement);
            this._conversations.add(conversationItem);
        }
    },
    
    startConversation: function MessengerApp__messengerApplication$startConversation(contact) {
        this._user.get_conversations().create(contact.get_currentAddress());
    },
    
    dispose: function MessengerApp__messengerApplication$dispose() {
        if (this._conversations) {
            var $enum1 = this._conversations.getEnumerator();
            while ($enum1.moveNext()) {
                var conversation = $enum1.get_current();
                conversation.dispose();
            }
            this._conversations = null;
        }
        if (this._contacts) {
            var $enum2 = this._contacts.getEnumerator();
            while ($enum2.moveNext()) {
                var contact = $enum2.get_current();
                contact.dispose();
            }
            this._contacts = null;
        }
        if (this._user) {
            this._user.dispose();
            this._user = null;
        }
    }
}


MessengerApp._contactItem.createClass('MessengerApp._contactItem', null, IDisposable);
MessengerApp._conversationItem.createClass('MessengerApp._conversationItem', null, IDisposable);
MessengerApp._messengerApplication.createClass('MessengerApp._messengerApplication', null, IDisposable);
MessengerApp._messengerApplication.current = null;
MessengerApp._messengerApplication.current = new MessengerApp._messengerApplication();

// ---- Do not remove this footer ----
// Generated using Script# v0.4.5.0 (http://projects.nikhilk.net)
// -----------------------------------
